import {Injectable} from '@angular/core';
import {HttpService} from "../../lib/service/http.service";
import {adSystemApiUrl, appletApiUrl} from '../../../config/api';
import {LocalStorageService} from "../../lib/service/local-storage.service";
import {Headers, RequestOptions} from "@angular/http";
import {AdOssUploadService} from "../../lib/service/ad-oss-upload.service";
import {Observable} from "rxjs/Observable";

@Injectable()
export class DisplayService {

  constructor(private httpService: HttpService, private localStorageService: LocalStorageService) {
  }

  id;
  list = [];
  showCaseId;
  showCaseIndex;
  boothNumber;
  name;


  IdObservable = Observable
    .create((observer) => {
      observer.next(this.id);
    });

  //初始化
  initInfo(pointer, booth) {
    switch (booth) {
      case 'a':
        this.boothA(pointer);
        break;
      case 'b':
        this.boothB(pointer);
        break;
      case 'c':
        this.boothC(pointer);
        break;
    }
  }

  boothA(pointer) {
    pointer.picSizeInfo = '图片尺寸: 710 * 330px';
    pointer.previewTitle = '首页轮播展示图';
    pointer.previewsubTitle = '可用于活动、商品推广，图片数量（0/3)';
    pointer.previewSrc = 'banner_view';
    pointer.options = [
      {value: 'picture', label: '图片'},
      {value: 'product', label: '商品'},
      {value: 'coupons', label: '卡券'},
      {value: 'activity', label: '活动'},
      {value: 'page', label: '页面'}
    ];
  }

  boothB(pointer) {
    pointer.picSizeInfo = '图片尺寸: 750 * 232px';
    pointer.previewTitle = '商品推荐展示图';
    pointer.previewsubTitle = '用于商品宣传推广';
    pointer.previewSrc = 'special_view';
    pointer.options = [
      {value: 'product', label: '商品'},
      // {value: 'coupons', label: '卡券'},
      {value: 'activity', label: '活动'},
      // {value: 'page', label: '页面'}
    ];
  }

  boothC(pointer) {
    pointer.picSizeInfo = '图片尺寸: 220 * 250px';
    pointer.previewTitle = '拼团展示图';
    pointer.previewsubTitle = '可用于拼团、抽奖商品推广';
    pointer.previewSrc = 'group_view';
    pointer.options = [{value: 'picture', label: '图片'}];
    pointer.selectedOption = pointer.options[0];
  }


  getCaseDetailList(showCaseId, index?) {
    return new Promise((resolve, reject) => {
      this.getShowCaseDetailList(showCaseId).subscribe(res => {
        if (res.code === 2000) {
          let detailList = this.list[index ? index : this.showCaseIndex]['detailList'] = res.data.data;
          detailList.map((e, i) => {
            e['edit'] = false;
          });
          let id = detailList.length != 0 ? detailList[0]['id'] : null;
          resolve(id);
        } else {
          reject();
        }
      })
    })
  }


  //获取橱窗列表
  getShowCaseList(templateId) {
    const url = `${adSystemApiUrl}/v1/member/show_case/list/?order={"sort":"asc"}&template_id=${templateId}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //根据showcaseId获取橱窗详情列表
  getShowCaseDetailList(showcaseId) {
    const url = `${adSystemApiUrl}/v1/member/show_case_detail/list?order={"sort":"asc"}&showcase_id=${showcaseId}&pageSize=999`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //根据showcaseId获取橱窗详情列表分页
  getShowCaseDetailCutList(showcaseId,page,pageSize,order,name) {
    const url = `${adSystemApiUrl}/v1/member/show_case_detail/list?order=${order}&showcase_id=${showcaseId}&name=${name}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //获取活动列表
  getActivityList(page, pageSize, keyWord, type, stage, status) {
    const url = `${adSystemApiUrl}/v3/member/activity/list?page=${page}&pageSize=${pageSize}&keyword=${keyWord}&type=${type}&stage=${stage}&status=${status}&order_by={"start_date":"desc","created_at":"desc"}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //获取活动商品列表
  getActivityGoodsList(activityId, activityGoodsId) {
    const url = `${adSystemApiUrl}/v3/member/activity_goods/get_goods?activity_id=${activityId}&activity_goods_id=${activityGoodsId}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }


  //获取卡券列表
  getCouponList(page, pageSize, keyWord) {
    const url = `${adSystemApiUrl}/v2/member/card_coupons/list`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(
      {
        'applet_uuid': this.localStorageService.getNowApplet()['applet_uuid'],
        'page': page,
        'pageSize': pageSize,
        'keyword': keyWord
      }
    );
    return this.httpService.httpPostAuth(url, body, options).map(res => res);
  }


  getShowCaseDetailInfo(id) {
    const token = this.localStorageService.getToken()['access_token'];
    const url = `${adSystemApiUrl}/v1/member/show_case_detail/${id}/info?token=${token}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }

  //获取商品详情
  getGoodsDetail(id) {
    const url = `${adSystemApiUrl}//v1/applet/goods/get_goods_info_v2?id=${id}`;
    return this.httpService.httpGetAuth(url).map(res => res);
  }





  //获取橱窗列表
  getBannerList(shop_id, goto_type) {
    const url = `${appletApiUrl}/backend/banner/index/?shop_id=${shop_id}&goto_type=${goto_type}`;
    let body = {'shop_id': null, 'goto_type': null};
    body.shop_id = shop_id;
    if (goto_type != null) {
      body.goto_type = goto_type;
    }
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  //添加展位单元
  addShowCase(data) {
    const url = `${appletApiUrl}/backend/banner/create`;
    // const headers = new Headers({'Content-Type': 'application/json'});
    // const options = new RequestOptions({headers: headers});
    let body = data;
    // body['applet_uuid'] = this.localStorageService.getNowApplet()['applet_uuid'];
    return this.httpService.httpPostAuth(url, body).map(res => res);
  }

  //展位删除
  delShowCaseDetail(id) {
    const url = `${appletApiUrl}/backend/banner/delete`;
    const shop_id = this.localStorageService.getShop()['id'];
    return this.httpService.httpPostAuth(url, {'id': id, 'shop_id' : shop_id}).map(rs => rs);

  }


  //修改展位详情
  editShowCaseDetail(data) {
    const url = `${appletApiUrl}/backend/banner/update`;
    // const headers = new Headers({'Content-Type': 'application/json'});
    // const options = new RequestOptions({headers: headers});
    let body = data;
    // body['applet_uuid'] = this.localStorageService.getNowApplet()['applet_uuid'];
    return this.httpService.httpPostAuth(url, body).map(res => res);

  }
}
